<template>
  <div class="todo-footer">
    <label>
      <input type="checkbox" v-model="allChecked" />
    </label>
    <span>
      <span>已完成{{ completeNum }}</span> / 全部{{ sumNum }}
    </span>
    <button class="btn btn-danger" @click="emit('clearComplete')">
      清除已完成任务
    </button>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'Footer',
});
</script>

<script setup lang="ts">
// 引入
import { computed } from 'vue';
import type { todoListType } from '../../App';

// 接收props
const props = defineProps<{
  todoList: todoListType;
}>();

// 接收自定义事件
const emit = defineEmits<{
  (event: 'allChecked', checkType: boolean): void;
  (event: 'clearComplete'): void;
}>();

// 已完成的计算属性
const completeNum = computed(() => {
  return props.todoList.reduce((p, c) => (c.done ? p + 1 : p), 0);
});

// 合计数量的计算属性
const sumNum = computed(() => {
  return props.todoList.length;
});

// 全选功能的可读可写的计算属性
const allChecked = computed({
  get() {
    return completeNum.value === props.todoList.length;
  },
  set(checkType: boolean) {
    emit('allChecked', checkType);
  },
});
</script>

<style scoped lang="scss">
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>
